import {messages} from "share/common";
import React, { Component } from 'react'
import { Form, Switch, Input, Button, message } from 'antd'
import Selector from 'components/selector'
import constant from "../constant";
import SettingService from 'containers/finance-setting/loan-setting/setting-service'

class PayTypeRule extends Component {

  state = {
    saveLoading: false
  };

  handleSave = () => {
    this.props.form.validateFieldsAndScroll((err, value) => {
      if (!err) {
        this.setState({ saveLoading: true });
        const { record } = this.props.params, { close } = this.props;
        SettingService.savePayTypeService(Object.assign({ versionNumber: 1 }, record, value)).then(() => {
          this.setState({ saveLoading: false }, () => {
            message.success(messages('common.operate.success')/*操作成功*/);
            close(true);
          });
        }).catch(e => {
          message.error(e.response.data.message);
          this.setState({ saveLoading: false });
        })
      }
    })
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { saveLoading } = this.state;
    const { formItemLayout } = constant;
    const { close } = this.props, { record } = this.props.params;
    return(
      <div>
        <Form className={'new-form'}>
          <Form.Item
            label={messages('common.column.status')/*状态*/}
            {...formItemLayout}
          >
            {getFieldDecorator("isEnabled", {
              rules: [{ required: true }],
              valuePropName: "checked",
              initialValue: record.hasOwnProperty('isEnabled') ? record.isEnabled : true
            })(
              <Switch>{messages('finance-1.key244')/*启用temp*/}</Switch>
            )}
          </Form.Item>
          <Form.Item
            label={messages('finance-1.key26')/*付款方式类型*/}
            {...formItemLayout}
          >
            {getFieldDecorator("paymentMethodCategory", {
              rules: [{ required: true , message: messages('common.select')/*请选择*/}],
              initialValue: Object.keys(record).length > 0 ? { key: record.paymentMethodCategory, label: record.paymentMethodCategoryName } : undefined
            })(
              <Selector
                disabled={record.paymentMethodCategory}
                type='value_list'
                itemMap
                params={{ type: 2105 }}
              />
            )}
          </Form.Item>
          <Form.Item
            label={messages('finance-1.key430')/*付款方式编码*/}
            {...formItemLayout}
          >
            {getFieldDecorator("paymentMethodCode", {
              rules: [{ required: true, message: messages('common.please.input')/*请输入*/ }],
              initialValue: record.paymentMethodCode
            })(
              <Input placeholder={messages('finance-1.key76')/*请输入temp2*/} disabled={record.paymentMethodCode}/>
            )}
          </Form.Item>
          <Form.Item
            label={messages('finance-1.key258')/*付款方式名称*/}
            {...formItemLayout}
          >
            {getFieldDecorator("description", {
              rules: [{ required: true, message: messages('common.please.input')/*请输入*/ }],
              initialValue: record.description
            })(
              <Input placeholder={messages('finance-1.key76')/*请输入temp2*/}/>
            )}
          </Form.Item>
          <div className='footer-operate'>
            <Button type='primary' onClick={this.handleSave} loading={saveLoading}>{messages('common.ok')/*确定*/}</Button>
            <Button onClick={() => close()}>{messages('common.cancel')/*取消*/}</Button>
          </div>
        </Form>
      </div>
    )
  }

}

export default Form.create()(PayTypeRule)
